package cn.whitecao.agent.core.config;

import cn.hutool.core.io.FileUtil;

import java.io.IOException;
import java.util.Properties;

/**
 * 配置文件
 *
 * @author : BashfulHuang
 **/
public class ConfigContext {
    private final static Properties PROPERTIES =new Properties();

    /**
     * 加载配置文件
     * @param filePath 文件路径
     */
    public static void load(String filePath){
        try {
            PROPERTIES.load(FileUtil.getInputStream(filePath));
        } catch (IOException e) {
            throw new RuntimeException("加载配置文件错误: "+filePath);
        }
    }

    /**
     * 获取Integer 类型
     * @param key key
     * @param defaultValue 默认值
     * @return Integer返回值
     */
    public static Integer getInt(String key,Integer defaultValue){
        try {
            String s = get(key);
            if (s==null){
                return defaultValue;
            }else {
                return new Integer(s);
            }
        } catch (NumberFormatException e) {
            return defaultValue;
        }
    }

    /**
     * 获取String类型
     * @param key key
     * @param defaultValue 默认值
     * @return  String 返回值
     */
    public static String getStr(String key, String defaultValue){
        String s = get(key);
        return s==null?defaultValue:s;
    }



    private static String get(String key){
        String property = PROPERTIES.getProperty(key);
        if (property==null){
            return null;
        }
        property=property.replace(" ","");
        if (property.isEmpty()){
            return null;
        }
        return property;
    }
}
